package com.dolawing.phonecallapp.blacknumber;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.dolawing.phonecallapp.R;
import com.dolawing.phonecallapp.blacknumber.db.BlackNumberDao;
import com.dolawing.phonecallapp.blacknumber.pojo.BlackNumber;

import java.util.List;

public class BlackNumberAdapter extends BaseAdapter {

    private Context context = null;
    private int resourceId = 0;
    private List<BlackNumber> listData = null;
    private BlackNumberDao blackNumberDao;

    public BlackNumberAdapter(Context context, int textViewResourceId , List<BlackNumber> object){
        this.context =context;
        resourceId = textViewResourceId;
        listData = object;
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setBlacknumbers(List<BlackNumber> object){
        listData = object;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //非Activity界面类中，获得布局的方法
        ViewHolder viewHolder = null;
        if(convertView==null){
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            convertView = layoutInflater.inflate(resourceId,null);
            //获得布局控件，存入fruitInfo对象中
            viewHolder = new ViewHolder();
            viewHolder.viewId = (TextView) convertView.findViewById(R.id.itemNoView);
            viewHolder.viewBlankNumber = (TextView)convertView.findViewById(R.id.itemBlankNumberView);
            viewHolder.deleteBtn = (Button) convertView.findViewById(R.id.itemDeleteBlankNumberBtn);
            convertView.setTag(viewHolder);
        }else
            viewHolder = (ViewHolder) convertView.getTag();
        //为布局控件赋值
        viewHolder.viewId.setText("No."+(listData.get(position).getId()));
        viewHolder.viewBlankNumber.setText(listData.get(position).getBlackNumber());
        //添加button的单击事件
        viewHolder.deleteBtn.setOnClickListener(new myClick(convertView, viewHolder.viewBlankNumber));
        if(position % 2==0)
            convertView.setBackgroundColor(Color.argb(40,0,255,100));
        return  convertView;
    }

    class ViewHolder{
        public TextView viewId;
        public TextView viewBlankNumber;
        public Button deleteBtn;
    }

    //方法二 选中记录，记录颜色反选效果  ****************编码开始
    class myClick implements  Button.OnClickListener{
        View view = null;
        TextView txtName;
        public myClick(View view , TextView txt){
            this.view = view;
            this.txtName = txt;
        }
        public void onClick(View v){
            view.setBackgroundColor(Color.RED);
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setMessage(txtName.getText()+":你确定删除吗?");
            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    blackNumberDao.delete(txtName.getText().toString().trim());
                    Toast.makeText(context,txtName.getText()+",删除成功",Toast.LENGTH_SHORT).show();
                }
            });
            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(context,txtName.getText()+",不删除！",Toast.LENGTH_SHORT).show();
                }
            });
            builder.show();
        }
    }
}
